package vip.liux.contracts.models.identity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import vip.liux.domain.entities.AggregateRoot;

import java.util.Set;


@Entity
@Table(name = "identity_roles", indexes = {
        @Index(name = "ix_identity_roles_normalized_name", columnList = "normalizedName")
})
public class IdentityRole extends AggregateRoot<Long, IdentityRole> {

    @Getter
    @Setter
    @Column(name = "tenant_id", length = 36)
    private String tenantId;

    @Getter
    @Setter
    @Column(name = "name", nullable = false, length = 256)
    private String name;

    @Getter
    @Setter
    @Column(name = "normalized_name", nullable = false, length = 256)
    private String normalizedName;

    @Column(name = "is_default", nullable = false)
    private boolean defaultRole = false;

    @Column(name = "is_static", nullable = false)
    private boolean staticRole = false;

    @Column(name = "is_public", nullable = false)
    private boolean publicRole = false;

    @Getter
    @Setter
    @ManyToMany(mappedBy = "roles")
    @JsonBackReference
    private Set<IdentityUser> users;

    public boolean isDefault() {
        return defaultRole;
    }

    public void setDefault(boolean defaultRole) {
        this.defaultRole = defaultRole;
    }

    public boolean isStatic() {
        return staticRole;
    }

    public void setStatic(boolean staticRole) {
        this.staticRole = staticRole;
    }

    public boolean isPublic() {
        return publicRole;
    }

    public void setPublic(boolean publicRole) {
        this.publicRole = publicRole;
    }

    public String getDisplayName() {
        return getExtraProperties().getOrDefault("displayName", getName()).toString();
    }
}